package com.filetest;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Scanner;


public class FileTest {
    public static void main(String[] args) throws IOException {

        //test1
        for (int i = 1; i <= 100; i++) {
            File file = new File("./" + i + ".txt");
            if (!file.exists()) {
                file.createNewFile();
            }
        }
        //test2
        for (int i = 1; i <= 100; i++) {
            File file = new File("./" + i + ".txt");
            if (file.exists()) {
                file.delete();
            }
        }

        //test5
        addFilePlus();

        //test6
        File file = new File(".");
        File[] arr = arrFileLambda(file);
        for (File file1 : arr ){
            System.out.println(file1.getName());
        }


    }
    //匿名内部类 过滤 test6
    public static File[] arrFile(File file){
        File[] arrFile = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().contains("s");
            }
        });        return arrFile;
    }

    //lambda表达式 过滤  test6
    public static File[] arrFileLambda(File file){
        File[] arrFile = file.listFiles(pathname -> pathname.getName().contains("s"));
        return arrFile;
    }

    //判断输入的格式是否正确
    public static boolean flag(String inputName) {
        String s = ("\\d{1,}.[a-z]{3}");
        if (inputName.matches(s)) {
            return true;
        }
        return false;
    }

    //添加plus  test5
    public static boolean addFilePlus() throws IOException {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要添加的文件名");
            String input = sc.next();
            File file = new File("./" + input + "");
            //判断格式
            boolean flag = flag(input);
            //判断是否存在
            boolean flag1 = contains(file);
            if (!flag) {
                System.out.println("文件名格式不正确，请重新输入");
                continue;
            }
            if (flag1) {
                System.out.println("文件已经存在，请重新输入");
                continue;
            }
            file.createNewFile();
            System.out.println("创建成功");
            return true;
        }
    }

    //添加文件 test3
    public static boolean addFile(String inputName) throws IOException {
        File file = new File("./" + inputName + "");
        if (!file.exists()) {
            file.mkdirs();
            System.out.println("添加成功");
            return true;
        } else {
            System.out.println("已经存在，添加失败");
            return false;
        }
    }

    //删除文件  test4
    public static boolean deleteFile() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要删除的文件名");
            String input = sc.next();
            File file = new File("./" + input + "");
            //判断格式
            boolean flag = flag(input);
            //判断是否存在
            boolean flag1 = contains(file);
            if (!flag) {
                System.out.println("文件名格式不正确，请重新输入");
                continue;
            }
            if (!flag1) {
                System.out.println("文件不存在，请重新输入");
                continue;
            }
            file.delete();
            System.out.println("删除成功");
            return true;
        }
    }

    //判断文件是否唯一
    public static boolean contains(File file) {
        if (file.exists()) {
            return true;
        } else {
            return false;
        }
    }
}
